//https://leetcode.cn/problems/multiply-strings/description/
//字符串相乘

class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1 == "0" || num2 == "0")
        return "0";
        
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());
        for (size_t i = 0; i < num1.size() || i < num2.size(); i++)
        {
            if (i < num1.size())
                num1[i] -= '0';
            if (i < num2.size())
                num2[i] -= '0';
        }

        string ret;
        ret.resize(num1.size() + num2.size(), 0);
        for (size_t pos1 = 0; pos1 < num1.size(); pos1++)
        {
            for (size_t pos2 = 0; pos2 < num2.size(); pos2++)
            {
                int pos = pos1 + pos2;
                ret[pos] += num1[pos1] * num2[pos2];
                //进位
                while (pos < ret.size() && ret[pos] >= 10)
                {
                    ret[pos + 1] += ret[pos] / 10;
                    ret[pos] %= 10;
                    pos++;
                }
            }
        }
        if (ret[ret.size() - 1] == 0)
            ret.erase(ret.size() - 1);

        reverse(ret.begin(), ret.end());
        for (size_t i = 0; i < ret.size(); i++)
        {
            ret[i] += '0';
        }

        return ret;
    }
};